/* 学习目标：取数据、渲染界面  */
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getNewsByIdAction } from '../store/action/news';
import defaultCover from '../assets/defaultCover.jpg';

export default function News() {
  const dispatch = useDispatch();
  const { channelId } = useSelector((state) => state.channel);
  // 1. 取数据
  const { list } = useSelector((state) => state.news);
  useEffect(() => {
    dispatch(getNewsByIdAction(channelId));
  }, [dispatch, channelId]);

  return (
    <div className="list">
      {list.map((item) => {
        // 2. 列表渲染
        return (
          <div key={item.art_id} className="article_item">
            <h3 className="van-ellipsis">{item.title}</h3>
            <div className="img_box">
              <img
                src={item.cover.type ? item.cover.images[0] : defaultCover}
                className="w100"
                alt=""
              />
            </div>
            <div className="info_box">
              <span>{item.aut_name}</span>
              <span>{item.comm_count}评论</span>
              <span>{item.pubdate}</span>
            </div>
          </div>
        );
      })}
    </div>
  );
}
